package datastruct.tree.binarytree;

import lombok.*;

/**
 * @program: DataStructuresAndAlgorithms
 * @description: 二叉树
 * @author: zhang
 * @create: 2020-03-10 17:17
 */
public class BinaryTreeDemo {

    public static void main(String[] args) {
        //创建一颗二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建所需要的节点
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node1 = new HeroNode(2,"吴用");
        HeroNode node2 = new HeroNode(3,"卢俊义");
        HeroNode node3 = new HeroNode(4,"林冲");
        HeroNode node4 = new HeroNode(5,"宋江");

        binaryTree.setRoot(root);

        root.setLeft(node1);
        root.setRight(node2);
        node2.setLeft(node3);
        node2.setRight(node4);


//        System.out.println("---------前序遍历------------");
//        binaryTree.preOrder();
//
//        System.out.println("---------中序遍历------------");
//        binaryTree.infixOrder();
//
//        System.out.println("---------后续遍历------------");
//        binaryTree.postOrder();
//
//
//        System.out.println("-----------前序查找----------------");
//        HeroNode heroNode = binaryTree.preOrderSearch(1);
//        if (heroNode != null){
//            System.out.println("找到了 信息为: "+heroNode);
//        }else {
//            System.out.println("没找到");
//        }


        System.out.println("--------------演示删除节点---------------");
        System.out.println("--------------删除前遍历---------------");
        binaryTree.preOrder();
        binaryTree.delNode(3);
        System.out.println("--------------删除后遍历---------------");
        binaryTree.preOrder();
    }

}

@Data
class BinaryTree{
    private HeroNode root;

    public void delNode(int no){
        if (root != null){
            if(no == root.getNo()){
                root = null;
            }else {
                root.delNode(no);
            }
        }else {
            System.out.println("您删除的是空树!!!");
        }

    }

    public void preOrder(){
        if (this.root != null){
            this.root.preOrder();
        }else {
            System.out.println("二叉树为空，无法遍历");
        }
    }
    public void infixOrder(){
        if (this.root != null){
            this.root.infixOrder();
        }else {
            System.out.println("二叉树为空，无法遍历");
        }
    }
    public void postOrder(){
        if (this.root != null){
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    public HeroNode preOrderSearch(int no){
        if (this.root != null){
            return this.root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode infixOrderSearch(int no){
        if (this.root != null){
            return this.root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode postOrderSearch(int no){
        if (this.root != null){
            return this.root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}

@Data
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }


    //删除节点
    public void delNode(int no){
        if (this.left != null && no == this.left.no){
            this.left = null;
            return;
        }
        if (this.right != null && no == this.right.no){
            this.right = null;
            return;
        }
        //递归左子树查找并删除
        if (this.left != null){
            this.left.delNode(no);
        }
        //递归右子树查找并删除
        if (this.right != null){
            this.right.delNode(no);
        }
    }
    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if (this.left != null){
            this.left.preOrder();
        }
        if (this.right != null){
            this.right.preOrder();
        }
    }
    //中序遍历
    public void infixOrder(){
        if (this.left != null){
            this.left.preOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.preOrder();
        }
    }
    //后序遍历
    public void postOrder(){
        if (this.left != null){
            this.left.postOrder();
        }
        if (this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preOrderSearch(int no){
        System.out.println("进入前序查找~~~");
        if (this.no == no){
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        if (this.right != null){
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }
    //中序遍历查找
    public HeroNode infixOrderSearch(int no){

        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        System.out.println("进入中序查找~~~");
        if (this.no == no){
            return this;
        }
        if (this.right != null){
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
    //后序遍历查找

    public HeroNode postOrderSearch(int no){

        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        if (this.right != null){
            resNode = this.right.infixOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        System.out.println("进入后序查找~~~");
        if (this.no == no){
            return this;
        }
        return null;
    }
}